package edu.zzuli.model.core.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.zzuli.common.Constants_core;
import edu.zzuli.common.MisException;
import edu.zzuli.common.MyVisit;
import edu.zzuli.model.core.iface.LoginService;
import edu.zzuli.model.dao.BaseDaoEntity;
import edu.zzuli.model.orm.po.GG_CZYB;
import edu.zzuli.model.orm.po.GG_JGBH;
import edu.zzuli.model.orm.po.GG_XTRZ;
import edu.zzuli.util.DateUtil;

/**
 * @author tianshaojie
 * @date 2011-1-6
 * @discription : 
 */
@Transactional
@Service("loginService")
public class LoginServiceImpl implements LoginService {
	
	@Resource
	private BaseDaoEntity baseDaoEntity;
	
	public Map saveLoginInfo(String dlh, String remoteIp) {
		GG_CZYB gg_CZYB = new GG_CZYB();
		gg_CZYB.setDlh(dlh);
		Map loginInfo = new HashMap();
		gg_CZYB = (GG_CZYB) this.baseDaoEntity.selectSingle(gg_CZYB, gg_CZYB.getStrExactWhere());
//		Iterator iterator = gg_CZYB.getChildGG_CZYB_GWLB().iterator();
//		while (iterator.hasNext()) {
//			((GG_CZYB_GWLB) iterator.next()).getGG_GWLB().getGwmc();
//		}
		loginInfo.put(MyVisit.USER_GG_CZYB, gg_CZYB);
		GG_JGBH gg_JGBH = (GG_JGBH) this.baseDaoEntity.getHibernateDao().selectSingle(new GG_JGBH(gg_CZYB.getGG_JGBH().getJgbh()));//daoFactory.getGG_JGBHDao().load(gg_CZYB.getGG_JGBH());
		GG_JGBH gg_JGBH_DW_Visit = new GG_JGBH();
		gg_JGBH_DW_Visit.setJgbh(gg_JGBH.getJgbh());
		gg_JGBH_DW_Visit.setJgmc(gg_JGBH.getJgmc());
		gg_JGBH_DW_Visit.setJglb(gg_JGBH.getJglb());
		gg_JGBH_DW_Visit.setJgbm(gg_JGBH.getJgbm());
		gg_JGBH_DW_Visit.setArea(gg_JGBH.getArea());

		String sql = "";
//		if (gg_JGBH_DW_Visit.getJglb() == Constants_core.JGSF_ZBJG) {
//			sql = "select distinct gg_lbfp.*\n" +
//				"  from gg_czyb_gwlb, gg_lbfp\n" + 
//				" where gg_lbfp.fpid = gg_czyb_gwlb.gwid\n" + 
//				"   and gg_czyb_gwlb.czyid = :GG_CZYID";
//		} else {
			sql = "select gg_lbfp.* from gg_lbfp  where fpid = :GG_CZYID";
//		}
		loginInfo.put(MyVisit.USER_GG_CZYLB, this.baseDaoEntity.getParaJdbcTemplate().queryForList(sql, new MapSqlParameterSource("GG_CZYID",gg_CZYB.getId())));
		loginInfo.put(MyVisit.DW_GG_JGBH, gg_JGBH_DW_Visit);
		loginInfo.put(MyVisit.LOGIN_TIME, DateUtil.getCurrentDateTimeView());
		Map mapPara = new HashMap();
		mapPara.put("SFDL", Constants_core.GG_CZYB_SFDL_YDL);
		mapPara.put("ID", gg_CZYB.getId());
		//系统不支持同一用户多次登录，是否登录状态改为未登录
//		if(Constants_xt.XT_PZCS_CSBS_TYHDCDL_NO == XtpzcsConstants.getSdzOfInteger(Constants_xt.XT_PZCS_CSBS_TYHDCDL)) {
//			baseDaoEntity.getParaJdbcTemplate().update("update gg_czyb set sfdl=:SFDL where id=:ID", mapPara);
//		}
		
		GG_XTRZ gg_XTRZ = new GG_XTRZ();
		gg_XTRZ.setCzyid(gg_CZYB.getId());
		gg_XTRZ.setName(gg_CZYB.getMc());
		gg_XTRZ.setDwbh(gg_JGBH_DW_Visit.getJgbh());
		gg_XTRZ.setArea(gg_JGBH_DW_Visit.getArea());
		gg_XTRZ.setRoletype(gg_JGBH_DW_Visit.getJglb());
		gg_XTRZ.setDlip(remoteIp);
		gg_XTRZ.setCzsj(Long.valueOf(DateUtil.formateCalendar(Calendar.getInstance(), "yyyyMMddHHmmss")));
		gg_XTRZ.setZxbz(Constants_core.GG_CZYB_SFDL_YDL);
		this.baseDaoEntity.getHibernateDao().save(gg_XTRZ);
//		daoFactory.getGG_XTRZDao().save(gg_XTRZ);
		
//		登录人员所属部门信息
		loginInfo.put(MyVisit.USER_GG_BMXX, this.getSsbmList(gg_CZYB.getId()));
		return loginInfo;
	}

	/**
	 * 根据人员id获取人员所属部门
	 * @param GG_CZYB_ID
	 * @return
	 */
	public List<Map<String, String>> getSsbmList(String GG_CZYB_ID) {
		try {
			if (GG_CZYB_ID == null) {
				throw new MisException("人员ID参数为null！");
			}
			Map map = new HashMap();
			String sql = "select GG_BMXX.Id,GG_BMXX.BMMC,GG_BMXX.BMFL from GG_BMRY,GG_BMXX where GG_BMRY.BMID=GG_BMXX.Id and GG_BMRY.RYID=:GG_CZYB_ID order by GG_BMXX.Sxh";
			map.put("GG_CZYB_ID", GG_CZYB_ID);
			return this.baseDaoEntity.queryForList(sql,map);
		} catch (MisException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new MisException("[LoginServiceImpl.getSsbmList]异常！");
		}
	}
	
	public List<Map> queryForList(String sql, MapSqlParameterSource paramMap) {
		return this.baseDaoEntity.queryForList(sql, paramMap);
	}
}
